package com.ai.controller.bt;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ai.common.controller.BaseController;
import com.ai.common.model.Pagination;
import com.ai.common.model.Result;
import com.ai.common.utils.lang.string.StringUtils;
import com.ai.mmd.reviewed.dto.SbhzinfoDTO;
import com.ai.mmd.reviewed.dto.SbmxinfoDTO;
import com.ai.mmd.reviewed.vo.SbmxinfoVO;
import com.ai.mmd.system.dto.GkinfoDTO;
import com.ai.mmd.system.dto.HwlxinfoDTO;
import com.ai.mmd.system.dto.XxinfoDTO;
import com.ai.mmd.system.dto.ZdinfoDTO;
import com.ai.model.member.info.dto.DataTablesDto;
import com.ai.service.reviewed.model.SbhzinfoModel;
import com.ai.service.reviewed.model.SbmxinfoModel;
import com.ai.service.reviewed.model.YhxxModel;
import com.ai.service.reviewed.service.ISbhzinfoService;
import com.ai.service.reviewed.service.ISbmxinfoService;
import com.ai.service.system.model.GkinfoModel;
import com.ai.service.system.model.HwlxinfoModel;
import com.ai.service.system.model.XxinfoModel;
import com.ai.service.system.model.ZdinfoModel;
import com.ai.service.system.service.IGkinfoService;
import com.ai.service.system.service.IHwlxinfoService;
import com.ai.service.system.service.IXxinfoService;
import com.ai.service.system.service.IZdinfoService;

@RequestMapping("/btsb")
@Controller("com.ai.controller.bt.SbmxController")
public class SbmxController extends BaseController{
	
	@Value("${mainDomain}")
	private String mainDomain;
	
	/**
	 * Description ：列表引导页
	 */
	@RequestMapping("/mxlistindex")
	public String mxlistindex(@RequestParam(required = true) String hzdh)throws Exception {
		
		YhxxModel yhxxModel = (YhxxModel)request.getSession().getAttribute("yhxxModel");
		if (yhxxModel==null) {
			return "/front/frontgoods/login";
		}
		
		Result result = new Result(true);
		result.addAttribute("hzdh", hzdh);
		result.rendering();
		
		return "/front/frontgoods/btsb/btsqmxlist";
	}
	
	/**
	 * 列表
	 * @param SbmxinfoDTO
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("mxlist")
	public DataTablesDto<SbmxinfoVO> mxlist(@ModelAttribute SbmxinfoDTO sbmxinfoDTO,
			@RequestParam(defaultValue = "0") Integer start)
			throws Exception {
		DataTablesDto<SbmxinfoVO> dto = new DataTablesDto<SbmxinfoVO>();
		
		YhxxModel yhxxModel = (YhxxModel)request.getSession().getAttribute("yhxxModel");
		if (yhxxModel==null) {
			List<SbmxinfoVO> list = new ArrayList<SbmxinfoVO>();
			dto.setRowsData(list);
			dto.setiTotalRecords(list.size());
			dto.setiTotalDisplayRecords(list.size());
			return dto;
		}
		
		int rows = 10;
		int page = (start / rows)+1;
		
		//获得明细列表
		sbmxinfoDTO.setPage(page);
		sbmxinfoDTO.setRows(rows);
		sbmxinfoDTO.setSfsc(0);
		Pagination<SbmxinfoVO> list = isbmxinfoService.getListVO(sbmxinfoDTO,mainDomain + "/upload/pictures/");
		
		//获得汇总表状态
		Map<String,Object> attach = new HashMap<String, Object>();
		SbhzinfoDTO sbhzinfoDTO = new SbhzinfoDTO();
		sbhzinfoDTO.setHzdh(sbmxinfoDTO.getHzdh());
		SbhzinfoModel sbhzinfoModel = isbhzinfoService.getOne(sbhzinfoDTO);
		if (sbhzinfoModel!=null) {
			attach.put("hzzt", sbhzinfoModel.getHzzt());
		}
		
		//存入集合
		dto.setRowsData(list.getRows());
		dto.setiTotalRecords(list.getTotal());
		dto.setiTotalDisplayRecords(list.getTotal());
		dto.setAttach(attach);
		return dto;
	}
	
	/**
	 * Description ：申请明细编辑引导页
	 */
	@RequestMapping("/mxeditindex")
	public String mxeditindex(@ModelAttribute SbmxinfoDTO sbmxinfoDTO)throws Exception {
		
		YhxxModel yhxxModel = (YhxxModel)request.getSession().getAttribute("yhxxModel");
		if (yhxxModel==null) {
			return "/front/frontgoods/login";
		}
		
		SbmxinfoVO sbmxinfoVO = isbmxinfoService.getVO(sbmxinfoDTO);
		//货物类型
		HwlxinfoDTO hwlxinfoDTO = new HwlxinfoDTO();
		hwlxinfoDTO.setSfsc(0);
		List<HwlxinfoModel> hwlxlist = ihwlxinfoService.getList(hwlxinfoDTO);
		
		//箱型
		XxinfoDTO xxinfoDTO = new XxinfoDTO();
		xxinfoDTO.setSfsc(0);
		List<XxinfoModel> xxlist =ixxinfoService.getList(xxinfoDTO);
		
		//港口
		GkinfoDTO gkinfoDTO = new GkinfoDTO();
		gkinfoDTO.setSfsc(0);
		List<GkinfoModel> gklist =igkinfoService.getList(gkinfoDTO);
		
		//站点
		ZdinfoDTO zdinfoDTO = new ZdinfoDTO();
		zdinfoDTO.setSfsc(0);
		List<ZdinfoModel> zdlist =izdinfoService.getList(zdinfoDTO);
		
		Result result = new Result(true);
		result.addAttribute("sbmxinfoModel", sbmxinfoVO);
		result.addAttribute("hwlxlist", hwlxlist);
		result.addAttribute("xxlist", xxlist);
		result.addAttribute("gklist", gklist);
		result.addAttribute("zdlist", zdlist);
		result.rendering();
		
		return "/front/frontgoods/btsb/editsqmx";
	}
	
	/**
	 * 编辑明细
	 * @param SbmxinfoDTO
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("editmx")
	public Map<String, Object> editmx(@ModelAttribute SbmxinfoDTO sbmxinfoDTO)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		YhxxModel yhxxModel = (YhxxModel)request.getSession().getAttribute("yhxxModel");
		if (yhxxModel==null||sbmxinfoDTO.getId()==null) {
			return map;
		}
		
		sbmxinfoDTO.setGxsj(new Date());
		SbmxinfoModel sbmxinfoModel = new SbmxinfoModel();
		BeanUtils.copyProperties(sbmxinfoDTO, sbmxinfoModel);
		isbmxinfoService.updateByPrimaryKey(sbmxinfoModel);
		
		map.put("status", 0);
		
		return map;
	}
	
    @RequestMapping("/querydh")
    @ResponseBody
    public Result querydh(HttpServletRequest request, @ModelAttribute SbmxinfoDTO sbmxinfoDTO)throws Exception {
        Result result = new Result(true);
        YhxxModel yhxxModel = (YhxxModel)request.getSession().getAttribute("yhxxModel");
        if (yhxxModel!=null) {
   		 sbmxinfoDTO.setYhid(yhxxModel.getYhid());
		}
        
        if(StringUtils.isNotBlank(sbmxinfoDTO.getYdhmh())){
        	 List<SbmxinfoModel> list = isbmxinfoService.getUnbindhwydhList(sbmxinfoDTO);
             if(CollectionUtils.isEmpty(list)) {
             	list = new ArrayList<SbmxinfoModel>();
             }
             result.addAttribute("rows", list);
        }else if (StringUtils.isNotBlank(sbmxinfoDTO.getHphmh())) {
        	List<SbmxinfoModel> list = isbmxinfoService.getUnbindtlhphList(sbmxinfoDTO);
            if(CollectionUtils.isEmpty(list)) {
            	list = new ArrayList<SbmxinfoModel>();
            }
            result.addAttribute("rows", list);
		}else if (StringUtils.isNotBlank(sbmxinfoDTO.getHgdhmh())) {
        	List<SbmxinfoModel> list = isbmxinfoService.getUnbindhgsbList(sbmxinfoDTO);
            if(CollectionUtils.isEmpty(list)) {
            	list = new ArrayList<SbmxinfoModel>();
            }
            result.addAttribute("rows", list);
		}
        
        return result;
    }
    
    /**
	 * 删除
	 * @param SbhzinfoDTO
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("del")
	public Map<String, Object> del(@ModelAttribute SbmxinfoDTO sbmxinfoDTO)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		SbmxinfoModel sbmxinfoModel = new SbmxinfoModel();
		sbmxinfoDTO.setSfsc(1);
		sbmxinfoDTO.setGxsj(new Date());
		BeanUtils.copyProperties(sbmxinfoDTO,sbmxinfoModel);
		isbmxinfoService.updateByPrimaryKey(sbmxinfoModel);
		
		map.put("status", 0);
		
		return map;
	}
	
	@Resource(name = "isbmxinfoService")
	private ISbmxinfoService isbmxinfoService;
	
	@Resource(name = "isbhzinfoService")
	private ISbhzinfoService isbhzinfoService;
	
	@Resource(name = "ihwlxinfoService")
	private IHwlxinfoService ihwlxinfoService;
	
	@Resource(name = "ixxinfoService")
	private IXxinfoService ixxinfoService;
	
	@Resource(name = "igkinfoService")
	private IGkinfoService igkinfoService;
	
	@Resource(name = "izdinfoService")
	private IZdinfoService izdinfoService;
}
